package gipfProject.games.zertz.gui;

import gipfProject.tools.ResourceLoader;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JPanel;

import gipfProject.games.zertz.data.ZertzData;
import gipfProject.games.zertz.engine.ZertzEngine;

public class ZertzUI extends JPanel {

	private ZertzData gameState;
	private ZertzEngine engine;

	private JLabel messageRule;

	BoardUI boardUI;
	DisplayMoveUI displayMoveUI;
	SelectMarbleUI selectMarbleUI;
	ScoreUI whiteScoreUI, blackScoreUI;

	static Color backgroundColor;

	public ZertzUI(JFrame parentFrame, ZertzData gameState, ZertzEngine engine) {
		this.gameState = gameState;
		this.engine = engine;

		// Load our resource files
		ResourceLoader images = new ResourceLoader("zertz");

		// Create the menu
		JMenuBar menu = new ZertzMenu(this,engine,parentFrame);

		// Specify the background color
		backgroundColor = new Color((float) 0.3, (float) 0.65, (float) 1);

		// Add the menu to the window
		parentFrame.setJMenuBar(menu);

		// Set the frame icon to an image loaded from a file.
		parentFrame.setIconImage(images.getResource("ZertzIcon"));

		// We will manually layout
		setLayout(null);

		// Set the window size
		setPreferredSize(new Dimension(670, 545));

		// Set the window's background color
		setBackground(backgroundColor);

		// Displays the board
		boardUI = new BoardUI(images, gameState, engine, this);
		add(boardUI);


		// Displays what move the player should make
		displayMoveUI = new DisplayMoveUI(images, gameState);
		add(displayMoveUI);

		
		// Displays what type of marble we are placing
		selectMarbleUI = new SelectMarbleUI(images, gameState);
		add(selectMarbleUI);

		
		// Displays the score
		whiteScoreUI = new ScoreUI(images, gameState, ZertzData.playerWhite);
		blackScoreUI = new ScoreUI(images, gameState, ZertzData.playerBlack);
		add(whiteScoreUI);
		add(blackScoreUI);
		
		// Displays any messages for the player
		messageRule = new JLabel("INIT ERROR", JLabel.CENTER);
		messageRule.setFont(new Font("Serif", Font.BOLD, 18));
		messageRule.setForeground(Color.red);
		messageRule.setBackground(Color.BLACK);
		messageRule.setOpaque(true);
		add(messageRule);

		//Layout the display
		messageRule.setBounds    (190,   5, 295,  25);
		
		whiteScoreUI.setBounds   ( 20,  45, 160, 265);
		boardUI.setBounds        (190,  35, 295, 295);	
		blackScoreUI.setBounds   (495,  45, 160, 265);
		
		selectMarbleUI.setBounds (178, 360, 320, 170);
		displayMoveUI.setBounds  ( 40, 390, 120, 100);

		
		refreshMessage();
	}// End application constructor

	private void relocateDisplayMoveUI() {
		if (gameState.getCurrentPlayer() == ZertzData.playerWhite){
			displayMoveUI.setBounds  ( 40, 390, 120, 100);
		}else{
			displayMoveUI.setBounds  (510, 390, 120, 100);
		}
	}
	
	protected void refreshDisplay() {
		boardUI.repaint();
		displayMoveUI.repaint();
		selectMarbleUI.repaint();
		whiteScoreUI.repaint();
		blackScoreUI.repaint();
		
		refreshMessage();
		relocateDisplayMoveUI();
	}

	public void refreshMessage() {
		String text = gameState.getErrorMessage();
		// Show the background if there is a message
		if (text == "") {
			messageRule.setOpaque(false);
		} else {
			messageRule.setOpaque(true);
		}

		// Update the actual JLabel w/ the message
		messageRule.setText(text);
	}
}
